package com.situ.meetingRoom.service.impl;

import com.situ.meetingRoom.dao.BoardroomDao;
import com.situ.meetingRoom.dao.BookingDao;
import com.situ.meetingRoom.dao.UserDao;
import com.situ.meetingRoom.dao.impl.BoardroomDaoImpl;
import com.situ.meetingRoom.dao.impl.BookingDaoImpl;
import com.situ.meetingRoom.dao.impl.UserDaoImpl;
import com.situ.meetingRoom.entity.Boardroom;
import com.situ.meetingRoom.entity.Booking;
import com.situ.meetingRoom.entity.User;
import com.situ.meetingRoom.service.BookingService;
import com.situ.meetingRoom.util.PageInfo;
import com.situ.meetingRoom.vo.BookingVO;

import java.util.List;

public class BookingServiceImpl implements BookingService {

private BookingDao bookingDao = new BookingDaoImpl();
private BoardroomDao boardroomDao = new BoardroomDaoImpl();
    @Override
    public PageInfo getForPage(Integer page, Integer limit, Booking booking) {
        return bookingDao.selectForPage(page,limit ,booking);
    }

    @Override
    public List<BookingVO> getAll(Booking booking) {
        return bookingDao.selectAll(booking);
    }

    @Override
    public int remove(Integer id) throws Exception {
        Booking booking = bookingDao.selectById(id);
        Boardroom boardroom = boardroomDao.selectById(booking.getBoardroomId());
        if (boardroom.getStatus().equals(1)){
            throw new Exception("该会议室未被占用");
        }
        boardroom.setStatus(1);
        boardroomDao.update(boardroom);
        //员工不存在
        if (booking == null) {
            throw new Exception("没有会议记录");
        }
        //员工状态
        if (booking.getStatus() == 1) {
            throw new Exception("会议记录已结束");
        }
        //设置员工状态
        booking.setStatus(1);

        //更新到数据库
        return bookingDao.update(booking);
    }

    @Override
    public int add(Booking booking) throws Exception {
        //        保证那唯一的字段不能重复
        //        保存到数据库
        Boardroom boardroom = boardroomDao.selectById(booking.getBoardroomId());
        if (boardroom.getStatus().equals(0)){
            throw new Exception("该会议室已被占用");
        }
        boardroom.setStatus(0);
        boardroomDao.update(boardroom);
        return bookingDao.insert(booking);

    }

    @Override
    public Booking getById(Integer id) {
        return bookingDao.selectById(id);
    }

    @Override
    public int edit(Booking booking) throws Exception {
        //有效性 电话不能冲突
        //部门ID验证
        return bookingDao.update(booking);
    }
}
